package organization

import (
	"MembershipRegist/db"
	"MembershipRegist/models"
	"time"
)

// 显示参与了某个活动的会员
func (c *Controller) ActivityMembers() {
	activityID, err := c.GetInt("activity_id")
	if err != nil {
		c.ResponseFailed(201, "activity_id 不合法")
		return
	}

	var activityMembers []models.OrganizationUserActivity
	db.DB.Preload("OrganizationActivity").
		Preload("OrganizationAdmin").Preload("User").
		Where("organization_activity_id = ?", activityID).Find(&activityMembers)
	result := make(map[string]interface{})
	result["status"] = 200
	result["message"] = "success"
	result["activity_members"] = activityMembers
	c.Data["json"] = result
	c.ServeJSON()
}

// 用户的活动参与状态修改
func (c *Controller) ActivityMemberVerification() {
	activityID, err := c.GetInt("activity_id")
	if err != nil {
		c.ResponseFailed(201, "activity_id 不合法")
		return
	}
	userID, err := c.GetInt("user_id")
	if err != nil {
		c.ResponseFailed(202, "user_id 不合法")
		return
	}

	newStatus, err := c.GetInt("new_status")
	if err != nil {
		c.ResponseFailed(203, "new_status 不合法")
		return
	}

	if newStatus < models.UserActivityToBeReviewed || newStatus > models.UserActivityRefuse {
		c.ResponseFailed(204, "new_status 不合法")
		return
	}

	db.DB.Model(&models.OrganizationUserActivity{}).
		Where("organization_activity_id = ? and user_id = ?", activityID, userID).
		Updates(map[string]interface{}{
			"organization_admin_id": c.OrganizationAdminID,
			"updated_at":            time.Now(),
			"status":                newStatus,
		})

	c.ResponseSuccess()
}
